package com.pingan.pastry.factory;

import com.pingan.pastry.rpc.SPI;
import com.pingan.pastry.rpc.support.HttpInvoker;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class HttpInvokerFactory implements InvokerFactory{

    public <T> T build(Class<?> targetClass, int connectionTimeout, int socketTimeout) {
        return this.createInvoker(targetClass, connectionTimeout, socketTimeout);
    }

    public <T> T build(Class<?> targetClass) {
        return this.createInvoker(targetClass, 0, 0);
    }

    public <T> T createInvoker(Class<?> targetClass, int connectionTimeout, int socketTimeout) {
        SPI annotation = targetClass.getAnnotation(SPI.class);
        String host = "";
        String requestType = "Get";
        if (annotation != null) {
            host = annotation.host();
            requestType = annotation.requestType();
        }
        InvocationHandler invoker = new HttpInvoker(host,  connectionTimeout, socketTimeout);
        return (T) Proxy.newProxyInstance(targetClass.getClassLoader(), new Class[]{targetClass}, invoker);
    }

}
